package net.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * TCP的接收端
 * @author Administrator
 *
 */
public class TCPReceiver {

	public static void main(String[] args) throws IOException {
		// 创建服务器端
		/*
		 * port:端口号
		 * backlog:最大能创建多少的链接，默认是50
		 * bindAddr:地址，默认是localhost
		 * 
		 * */
		ServerSocket socket = new ServerSocket(8888);
//		ServerSocket socket = new ServerSocket(port, backlog);
//		ServerSocket socket = new ServerSocket(port, backlog, bindAddr);
		
		// 等待链接
		Socket accept = socket.accept();
		
		// 两端交互使用的流
		InputStream inputStream = accept.getInputStream(); // 输入流
//		accept.getOutputStream(); // 输出流
		
		// 接收数据
		byte[] content = new byte[1024];
		int length = inputStream.read(content);
		System.out.println("接收到的数据是：" + new String(content, 0, length));
		
		// 关闭资源
		inputStream.close();
		accept.close();
		socket.close();
		
	}
	
}
